function varargout = Listrik(varargin)
% LISTRIK MATLAB code for Listrik.fig
%      LISTRIK, by itself, creates a new LISTRIK or raises the existing
%      singleton*.
%
%      H = LISTRIK returns the handle to a new LISTRIK or the handle to
%      the existing singleton*.
%
%      LISTRIK('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LISTRIK.M with the given input arguments.
%
%      LISTRIK('Property','Value',...) creates a new LISTRIK or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Listrik_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Listrik_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Listrik

% Last Modified by GUIDE v2.5 27-Apr-2011 22:39:00

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Listrik_OpeningFcn, ...
                   'gui_OutputFcn',  @Listrik_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Listrik is made visible.
function Listrik_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Listrik (see VARARGIN)

% Choose default command line output for Listrik
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Listrik wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Listrik_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in ListrikHitungButton_arus.
function ListrikHitungButton_arus_Callback(hObject, eventdata, handles)
global soal1;
global numsoal;
global metode;
global x0 y0 z0 xr yr zr h n1 n2;
global xn n y01 y02 numy0;
global fxyz1 fxyz2;
global fungsi1 fungsi2;
global numfungsi;
global metodestring;
global hasil1 hasil2; 
global numhasil;

fxyz1 = @(x,y,z)(z); % fungsi i' = y' = z
fxyz2 = @(x,y,z)((-4.7*z-y/(1000*10^-6))/(50*10^-3)); % fungsi i" = y" = z'
x0 = 0; xr = 0.1;
y0 = 15; % V
z0 = 0; % dV/dt
h = 0.002;
metode = get(handles.ListrikPopup_metode,'Value');

switch(metode)
    case{1}
        [n1 yr] = Euler(fxyz1,[x0 xr],[y0 z0],h);
        [n2 zr] = Euler(fxyz2,[x0 xr],[y0 z0],h);
        display(yr);
        display(zr);
        metodestring = 'Euler';
    case{2}
        [n1 yr] = Heun(fxyz1,[x0 xr],[y0 z0],h);
        [n2 zr] = Heun(fxyz2,[x0 xr],[y0 z0],h);
        display(yr);
        display(zr);
        metodestring = 'Heun';
    case{3}
        [n1 yr] = Runge_Kutta([x0 xr],h,[y0 z0],fxyz1,3);
        [n2 zr] = Runge_Kutta([x0 xr],h,[y0 z0],fxyz2,3);
        display(yr);
        display(zr);
        metodestring = 'Runge-Kutta Orde 3';
    case{4}
        [n1 yr] = Runge_Kutta([x0 xr],h,[y0 z0],fxyz1,4);
        [n2 zr] = Runge_Kutta([x0 xr],h,[y0 z0],fxyz2,4);
        display(yr);
        display(zr);
        metodestring = 'Runge-Kutta Orde 4';
    case {5}
        [n1 yr] = Adam_Moulton([x0 xr],h,[y0 z0],fxyz1);
        [n2 zr] = Adam_Moulton([x0 xr],h,[y0 z0],fxyz2);
        display(yr);
        display(zr);
        metodestring = 'P-C Adams-Moulton';        
end;

%Perhitungan V:
V = 50 * 10^-3 * zr(2) + 4.7 * yr(1) + 1.602176487 * 10^-19 / 1000*10^-6;

set(handles.ListrikOutput_arus, 'String', num2str(yr(1)));
set(handles.ListrikOutput_iabsen, 'String', num2str(zr(2),'%10.5e'));
set(handles.ListrikOutput_tegangan, 'String', num2str(V,'%10.5e'));

%------------------------------------------------%
% Pembuatan report
%------------------------------------------------%
% Soal
soal1 = 'L*i'''' + R*i''+ (1/C)*i = 0';
numsoal = 1;
% Variabel
% x0 = x0 (variabel dan globalnya sama)
xn = xr;
% h = h (variabel dan globalnya sama)
n = n2;
y01 = y0; y02 = z0;
numy0 = 2;
% Fungsi untuk perhitungan
fungsi1 = func2str(fxyz1);
fungsi2 = func2str(fxyz2);
numfungsi = 2;
% Hasil
hasil1 = yr(1);
hasil2 = V;
numhasil = 2;
% Generate Report
report('DifferentialEquation');

% --- Executes on selection change in ListrikPopup_metode.
function ListrikPopup_metode_Callback(hObject, eventdata, handles)
% hObject    handle to ListrikPopup_metode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns ListrikPopup_metode contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ListrikPopup_metode


% --- Executes during object creation, after setting all properties.
function ListrikPopup_metode_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ListrikPopup_metode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
